<template>
    <div class="app-container">
        <!-- 表格-条件筛区域  -->
        <div class="filter-container">

        </div>

        <!-- 表格-内容区域 -->
        <el-table v-loading="listLoading" :key="tableKey" :data="tableList" border fit highlight-current-row
                  style="width: 100%;">
            <el-table-column v-for="column in columns" :key="column.prop"
                             :prop="column.prop"
                             :label="column.label"
                             :width="column.width"
                             :align="column.align?column.align:'center'"
                             :min-width="column.minWidth"
                             :sortable="column.sortable"
                             :fixed="column.fixed"
                             :type="column.type" :formatter="column.formatter">
                <template slot-scope="scope">
                    <!-- 行内显示图片 -->
                    <img v-if="column.imgFlag==true"
                         :src=" scope.row[column.prop].indexOf('http://') >-1 ?  scope.row[column.prop] : ( CMBASE.baseFileContext + scope.row[column.prop] ) "
                         width="60" height="60" class="img" alt=""/>
                    <!-- 行内显示开关按钮 -->
                    <el-switch v-else-if="column.switchFlag==true" v-model="scope.row[column.prop]" :active-value="1"
                               :inactive-value="0" disabled></el-switch>
                    <!-- 行内显示tag标签 -->
                    <span v-else-if="column.tagFlag==true">
                        <span v-for="item in column.tagFormatter" :key="item.value">
                            <el-tag v-if="item.value==scope.row[column.prop]" :type="item.type">{{ item.text }}</el-tag>
                        </span>
                    </span>
                    <!-- 行内格式自定义 -->
                    <span v-else-if="column.formatter"> {{ column.formatter(scope.row) }}</span>
                    <!--默认显示文本-->
                    <span v-else>{{ scope.row[column.prop] }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="240" class-name="small-padding">
                <template slot-scope="scope">
                    <el-button type="primary" size="mini" @click="handleUpdate(scope.row)"> 查看</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 表格-页脚区域 -->
        <pagination v-show="tableTotal>0" :total="tableTotal" :page.sync="pageRequest.pageNum"
                    :limit.sync="pageRequest.pageSize" @pagination="getList"/>

        <!--新增编辑界面-->
        <el-dialog :title="operation?'查看':'查看'" width="45%" top="5vh" :visible.sync="dialogFormVisible"
                   :close-on-click-modal="false">
            <el-form :model="dataForm" label-width="140px" :rules="dataFormRules" ref="dataForm" style="width: 80%"
                     disabled>

                <el-form-item label="真实姓名" prop="userName">
                    <el-input v-model="dataForm.userName" auto-complete="off" placeholder="请输入真实姓名"/>
                </el-form-item>
                <el-form-item label="身份证号" prop="cardNumber">
                    <el-input v-model="dataForm.cardNumber" auto-complete="off" placeholder="请输入身份证号"/>
                </el-form-item>
                <el-form-item label="证件照正面" prop="certPositive">
                    <cm-upload v-model="dataForm.certPositive"></cm-upload>
                </el-form-item>
                <el-form-item label="证件照反面" prop="certReverse">
                    <cm-upload v-model="dataForm.certReverse"></cm-upload>
                </el-form-item>
                <el-form-item label="审核状态" prop="state">
                    <el-select v-model="dataForm.state" placeholder="请选择审核状态">
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="通过" :value="1"></el-option>
                        <el-option label="拒绝" :value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核备注信息" prop="remark">
                    <el-input v-model="dataForm.remark" auto-complete="off" placeholder="请输入审核备注信息"/>
                </el-form-item>
                <el-form-item label="创建时间" prop="ct">
                    <el-date-picker type="datetime" placeholder="选择日期" v-model="dataForm.ct"
                                    value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"
                                    style="width: 100%;"></el-date-picker>
                </el-form-item>
                <el-form-item label="审核时间" prop="et">
                    <el-date-picker type="datetime" placeholder="选择日期" v-model="dataForm.et"
                                    value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"
                                    style="width: 100%;"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button v-if="dataForm.state===0" size="mini" @click.native="handleApply(dataForm.id)"
                           :loading="editLoading" type="primary">拒绝
                </el-button>
                <el-button v-if="dataForm.state===0" size="mini" @click.native="saveFormData(dataForm.id)"
                           :loading="editLoading" type="primary">通过
                </el-button>
                <el-button v-if="dataForm.state===1 ||dataForm.state===2" size="mini" @click.native="closeDialog()">关闭
                </el-button>
            </div>
        </el-dialog>

        <el-dialog :title="checkName" :visible.sync="getCheckVisible">
            <el-form :model="checkForm" ref="checkForm">
                <el-form-item label="审核备注" prop="remark">
                    <el-input type="textarea" v-model="checkForm.remark" autocomplete="off"
                              placeholder="请输入审核备注" style="width: 80%"></el-input>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button size="mini" @click.native="handleSubmit()" type="primary">提交</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 分页栏信息
import {applyLive, getLiveApply} from '@/api/modules/live'
import CmUpload from '@/components/Upload'

export default {
    name: 'liveApply',
    components: {
        Pagination,
        CmUpload
    },
    directives: {},
    filters: {},
    data() {
        return {
            tableKey: 0,
            tableList: [],//表格数据源
            tableTotal: 0,//表格总条数数据
            listLoading: true,
            pageRequest: {pageNum: 1, pageSize: 10,},
            searchParam: {},
            dialogFormVisible: false,//默认不显示弹出框
            operation: true, // true:新增, false:编辑
            editLoading: false,//保存表单时loading框
            columns: [
                {prop: 'userName', label: '真实姓名', sortable: false},
                {prop: 'cardNumber', label: '身份证号', sortable: false},
                {prop: 'certPositive', label: '证件照正面', sortable: false, imgFlag: true},
                {prop: 'certReverse', label: '证件照反面', sortable: false, imgFlag: true},
                {
                    prop: 'state', label: '审核状态', sortable: false, tagFlag: true,
                    tagFormatter: [
                        {text: '待审核', value: 0, type: 'warning'},
                        {text: '通过', value: 1, type: 'success'},
                        {text: '拒绝', value: 2, type: 'error'}
                    ]
                },
            ],
            fileList: [],
            // 新增编辑界面数据
            dataForm: {},
            //表单检验规则
            dataFormRules: {
                userName: [{required: true, message: '请输入', trigger: 'blur'}],
                cardNumber: [{required: true, message: '请输入', trigger: 'blur'}],
                certPositive: [{required: true, message: '请输入', trigger: 'blur'}],
                certReverse: [{required: true, message: '请输入', trigger: 'blur'}],
                state: [{required: true, message: '请输入', trigger: 'blur'}],
            },
            applyId: '',
            //审核信息
            matchId: "",
            checkName: '审核备注',
            getCheckVisible: false, // 属性值列表页 隐藏
            // 新增编辑界面数据
            checkForm: {},
        }
    },
    methods: {
        //获取表格数据
        getList() {
            this.listLoading = true
            //【API请求】----获取表格列表数据
            getLiveApply(this.pageRequest).then(response => {
                response = response.result
                console.log(response);
                this.tableList = response.list
                this.tableTotal = response.total
                this.listLoading = false;
            }).catch((res) => {
                this.$message({message: res.msg, type: 'error'})
            })
        },
        //表格数据筛选
        handleFilter() {
            this.pageRequest.searchParam = this.searchParam
            this.getList()
        },
        //【按钮点击】--- 弹出编辑表单
        handleUpdate(row) {
            this.operation = false;
            this.dataForm = Object.assign({}, row)
            //显示编辑菜单
            this.dialogFormVisible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].clearValidate()
                this.editLoading = false;
            })
        },
        //保存表单数据
        saveFormData(id) {
            this.applyId = id
            const state = 1
            const params = {
                state: state,
                id: id,
                remark: "审核通过"
            }
            applyLive(params).then(response => {
                if(response.code === 500){
                    this.$notify({title: '失败', message: response.message, type: 'error', duration: 2000})
                    return
                }else if(response.code === 0){
                    this.getList()// 重新获取表格数据
                    this.$notify({title: '成功', message: '已审核通过', type: 'success', duration: 2000})
                    this.dialogFormVisible = false
                }

            }).catch(() => {
                this.dialogFormVisible = false
                this.getList()
            })
        },
        //点击拒绝， 输入拒绝原因
        handleApply(id) {
            this.matchId = id
            this.getCheckVisible = true
            this.dialogFormVisible = false
        },
        //点击提交，处理拒绝申请
        handleSubmit() {
            const state = 2
            const params = {
                state: state,
                id: this.matchId,
                remark: this.checkForm.remark
            }
            // 【API请求】----文章审核 拒绝
            applyLive(params).then(response => {
                this.getCheckVisible = false
                this.getList()// 重新获取表格数据
                this.$notify({title: '成功', message: '已审核拒绝', type: 'success', duration: 2000})
            }).catch(() => {
                this.getCheckVisible = false
                this.getList()
            })
        },
        //初始化表单数据，重新渲染
        initDataForm() {
            this.dataForm = {
                id: '',
                userId: '',
                userName: '',
                cardNumber: '',
                certPositive: '',
                certReverse: '',
                state: '',
                remark: '',
                ct: '',
                et: '',
                eu: '',
            };
        },
        //关闭表单弹出框
        closeDialog() {
            this.$refs['dataForm'].resetFields();
            this.dialogFormVisible = false
        },
    },
    created() {
        this.getList();
    },
}
</script>
